﻿using System.Windows.Forms;
using AForge.Video.DirectShow;
using System;

namespace CamPlug.WebCam.Plugin
{
	public partial class PropertiesForm : Form
	{
		readonly WebCamPlugin WebCamPlugin;

		public PropertiesForm(WebCamPlugin webCamPlugin)
		{
			InitializeComponent();
			this.WebCamPlugin = webCamPlugin;
			ComboBoxVideoDevices.Items.Add(new DeviceListBoxItem("<None>", null));
			foreach (FilterInfo videoDevice in WebCamPlugin.VideoDevices)
				if (string.Compare("CamPlug", videoDevice.Name, true) != 0)
					ComboBoxVideoDevices.Items.Add(new DeviceListBoxItem(videoDevice.Name, videoDevice.MonikerString));
		}

		void ComboBoxVideoDevices_SelectedIndexChanged(object sender, EventArgs e)
		{
			int selectedIndex = ComboBoxVideoDevices.SelectedIndex;
			if (selectedIndex == 0)
				WebCamPlugin.VideoSource = null;
			else
			{
				var selectedItem = (DeviceListBoxItem)ComboBoxVideoDevices.Items[selectedIndex];
				WebCamPlugin.VideoSource = new VideoCaptureDevice(selectedItem.MonikerString);
			}
		}

		void PropertiesForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = true;
			Hide();
		}

		void PropertiesForm_Shown(object sender, EventArgs e)
		{
			Location = Cursor.Position;
		}
	}
}
